package v1

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mycms/global"
	"mycms/model/common/response"
	systemReq "mycms/model/system/request"
	"mycms/service/admin"
	"mycms/service/common"
)

// @Tags 后台接口
// @Summary API管理列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param path formData string true "Api路径"
// @Param description formData string true "Api描述"
// @Param api_group formData string true "Api分组"
// @Param page formData int true "分页页码"
// @Param limit formData int true "每页数量"
// @Success 200 {object} systemRes.Apis
// @Router /admin/v1/apisList [get]
// @auth      xueneng
func (b *BaseApi)GetApisList(c *gin.Context)  {
	var param systemReq.SearchApiParams

	if ok := c.Bind(&param); ok != nil{
		//	返回错误信息
		response.FailWithMessage(ok.Error(), c)
		return
	}

	err, list, total := admin.GetApisList(param)

	if err != nil{
		global.LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	}else{
		response.OkWithDetailed(response.PageResult{
			List:   list,
			Total:  total,
			Page:   param.Page,
			Limit:  param.Limit,
		}, "查询成功", c)
	}
}


// @Tags 后台接口
// @Summary API现有分组
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object}
// @Router /admin/v1/getGroupList [get]
// @auth      xueneng
func (b *BaseApi)GetApisGroupList(c *gin.Context)  {
	err, list := admin.GetApisGroupList()

	if err != nil{
		global.LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	}else{
		response.OkWithDetailed(response.DataResult{
			Data:   list,
		}, "查询成功", c)
	}
}

// @Tags 后台接口
// @Summary 创建API
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object}
// @Router /admin/v1/apisCreate [get]
// @auth      xueneng
func (b *BaseApi)CreateApis(c *gin.Context)  {
	var param systemReq.CreateApiParams

	err := c.BindJSON(&param)

	if err != nil{

		response.FailWithMessage("API创建失败", c)
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	err = admin.CreateApis(param)

	if err != nil {
		global.LOG.Error("创建失败!", zap.Error(err))
		response.FailWithDetailed("", "创建失败："+err.Error(), c)
	} else {
		response.OkWithDetailed("", "创建成功", c)
	}

}


// @Tags 后台接口
// @Summary 更新API
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object}
// @Router /admin/v1/apisUpdate [put]
// @auth      xueneng
func (b *BaseApi)UpdateApis(c *gin.Context)  {
	var param systemReq.UpdateApiParams

	err := c.BindJSON(&param)

	if err != nil{
		response.FailWithMessage("API更新失败", c)
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	err = admin.UpdateApis(param)

	if err != nil {
		response.FailWithMessage("API更新失败："+err.Error(), c)
	} else {
		response.OkWithDetailed("", "API更新成功", c)
	}

}

// @Tags 后台接口
// @Summary 更新API
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "Api ID"
// @Success 200 {object}
// @Router /admin/v1/getApisInfo [get]
// @auth      xueneng
func (b *BaseApi)GetApisInfo(c *gin.Context)  {
	var param systemReq.ApisInfoParams

	err := c.Bind(&param)
	if err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}

	ok, apis := admin.GetApisInfo(param)
	if ok != nil{
		global.LOG.Error(ok.Error(), zap.Error(ok))
		response.FailWithMessage(ok.Error(), c)
		return
	}

	response.OkWithDetailed(apis, "查询成功", c)
}

// @Tags 后台接口
// @Summary 删除API
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id formData string true "Api ID"
// @Success 200 {object}
// @Router /admin/v1/delApis [delete]
// @auth      xueneng
func (b *BaseApi)DelApis(c *gin.Context)  {
	var param systemReq.DelApisParams

	err := c.Bind(&param)

	if err != nil{
		//	返回错误信息
		response.FailWithMessage(err.Error(), c)
		return
	}

	//	验证参数是否合法
	if valErr := common.Validate.Struct(param); valErr != nil{
		response.FailWithMessage(common.Translate(valErr, param), c)
		return
	}

	errs := admin.DelApis(param)

	if errs != nil {
		global.LOG.Error("删除失败!", zap.Error(errs))
		response.FailWithDetailed("", "删除失败："+errs.Error(), c)
	} else {
		response.OkWithDetailed("", "删除成功", c)
	}
}

// @Tags 后台接口
// @Summary 查询所有API
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} systemRes.Apis
// @Router /admin/v1/allApisList [get]
// @auth      xueneng
func (b *BaseApi)GetAllApisList(c *gin.Context)  {


	err, list := admin.GetAllApisList()

	if err != nil{
		global.LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	}else{
		response.OkWithDetailed(response.DataResult{
			Data:   list,
		}, "查询成功", c)
	}
}
